<?php

/* * 
 * 活动记录
 */
namespace Api\Model;
use Think\Model;
class ActivityRecordModel extends Model {

    //映射
    protected $_map = array(
        'activityId' => 'activity_id',
    );

    //添加活动记录
    public function addActivityRecord($userId, $photoJson) {

        //校验字段
        $validateRules = array(
            array('activity_id', 'require'),
            array('description', 'require')
        );

        //自动完成
        $autoRules = array(
            array('create_time', 'getCurrentDate',self::MODEL_INSERT,'function'),
            array('user_id', $userId),
            array('photo_json', $photoJson),
            array('collect_count', 0),
            array('comment_count', 0),
        );

        $activityRecord = $this->validate($validateRules)->auto($autoRules)->create();

        if(!$activityRecord) {
            $result['status'] = "ERROR_NECESSARY_DATA_NOT_FOUND";
            return $result;
        } else {

            $activity = D('activity')->getActivityById($activityRecord['activity_id']);
            if(isNullOrEmpty($activity)) {
                $result['status'] = "ERROR_ACTIVITY_NOT_FOUND";
                return $result;
            }

            $recordId = $this->add();

            $result['status'] = "SUCCESS";
            $result['activityRecordId'] = $recordId;

            return $result;
        }

    }

    //获取活动记录
    public function getActivityRecordById($id) {

        $activityRecord = $this->where(array('record_id' => $id))->find();

        return $activityRecord;

    }

    //根据活动ID获取单条活动记录（含评论）
    public function getActivityRecordSingle($activityId, $userId) {

        $map['ar.activity_id'] = $activityId;
        $activityRecord = $this->alias('ar')
                    ->join("LEFT JOIN __USER__ u ON ar.user_id = u.user_id")
                    ->field("ar.record_id, u.user_id, u.username, u.city, u.head_image_url, ar.description, ar.photo_json,
                    DATE_FORMAT(ar.create_time,'%m-%d %H:%i') as create_time")
                    ->where($map)
                    ->find();

        if(!isNullOrEmpty($activityRecord)) {
            $activityRecord['photo_list'] = unserializeToArray($activityRecord['photo_json']);
            unset($activityRecord['photo_json']);
            $activityRecord['is_collect'] = D('collect')->checkCollect($activityRecord['record_id'],C("COLLECT_TYPE.ACTIVITY_RECORD"),$userId);
            $activityRecord['is_follow'] = D('follow')->checkFollow($userId,$activityRecord['user_id']);
            $activityRecord['collect_list'] = D('collect')->getCollectUsers($activityRecord['record_id'], C("COMMENT_TYPE.ACTIVITY_RECORD"));
            $activityRecord['comment_list'] = D('comment')->getCommentList($activityRecord['record_id'], C("COMMENT_TYPE.ACTIVITY_RECORD"), 1);
        }

        return $activityRecord;

    }

    //根据活动ID获取活动记录（含评论）
    public function getActivityRecordList($activityId, $pageNum, $userId) {

        $map['ar.activity_id'] = $activityId;
        $list = $this->alias('ar')
            ->join("LEFT JOIN __USER__ u ON ar.user_id = u.user_id")
            ->field("ar.record_id, u.user_id, u.username, u.city, u.head_image_url, ar.description, ar.photo_json,
            DATE_FORMAT(ar.create_time,'%m-%d %H:%i') as create_time")
            ->where($map)
            ->order("ar.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        if(!isNullOrEmpty($list)) {

            foreach ($list as &$item) {
                $item['photo_list'] = unserializeToArray($item['photo_json']);
                unset($item['photo_json']);
                $item['is_collect'] = D('collect')->checkCollect($item['record_id'],C("COLLECT_TYPE.ACTIVITY_RECORD"),$userId);
                $item['is_follow'] = D('follow')->checkFollow($userId,$item['user_id']);
                $item['collect_list'] = D('collect')->getCollectUsers($item['record_id'], C("COMMENT_TYPE.ACTIVITY_RECORD"));
                $item['comment_list'] = D('comment')->getCommentList($item['record_id'], C("COMMENT_TYPE.ACTIVITY_RECORD"), 1);
            }

        }

        return $list;
    }

    public function getActivityRecordCountByActivityId($activityId) {

        $map['activity_id'] = $activityId;
        $count = $this->where($map)->count();

        return $count;

    }

}

